
@extends ('layouts/app')
@section('content')
    <div class="weui-pay append">
        <form name="info" id="info" method="POST">
            @csrf
            <input type="hidden" name="id" value="">
            <div class="site weui-cells_checkbox">
                {{--<div class="remaining"></div>空元素--}}
                {{--收件人--}}
                <div class="weui-cell ">
                    <div class="weui-cell__bd">
                        <input class="weui-input" type="text" name="name" placeholder="收件人">
                    </div>
                </div>
            </div>
            <div class="site weui-cells_checkbox">
                {{--联系电话--}}
                <div class="weui-cell">
                    <div class="weui-cell__bd">
                        <input class="weui-input" type="text" name="mobile" placeholder="手机号">
                    </div>
                </div>
            </div>
            <div class="site weui-cells_checkbox">
                {{--地区选择--}}
                <div class="weui-cell">
                    <div class="weui-cell__hd"><label for="name" class="weui-label">地址:</label></div>
                    <div class="weui-cell__bd">
                        <input class="weui-input" id="start" name="address" type="text" value="广东省 广州市 花都区" readonly="" data-code="420106" data-codes="440000,440100,440114">
                    </div>
                    <div class="weui-cell__ft"></div>
                    {{--<svg class="icon f28" aria-hidden="true">--}}
                        {{--<use xlink:href="#icon-tubiao-13"></use>--}}
                    {{--</svg>--}}
                </div>
            </div>
            <div class="site weui-cells_checkbox">
                <div class="weui-cells__title indetail">详细地址:</div>
                {{--详细地址--}}
                <div class="weui-cell">
                    <div class="weui-cell__bd">
                        <input class="weui-input" name="address_detail" type="text" placeholder="详细地址:如道路,门牌号,小区,楼栋号等">
                    </div>
                </div>
            </div>
            {{--保存按钮--}}
            <div class="demos-content-padded">
                <a href="javascript:;" class="weui-btn weui-btn_primary_a btn-confirm">确定</a>
            </div>


        </form>
    </div>

    {{--所在区域组件--}}
    <script type="text/javascript" src="/jquery-weui/dist/js/city-picker.js" charset="utf-8"></script>
    <script>
        $(".next1").click(function () {
            $("#full1").popup();
        });

        //添加地址选择区域
        $("#start").cityPicker({
            title: "选择地区",
            onChange: function (picker, values, displayValues) {
                console.log(values, displayValues);
            }
        });

        function getAddressList () {
            $.post("/api/user/allAddress", {}, function (res) {
                console.log("res", res);
                if (typeof res === 'string') {
                    res = JSON.parse(res);
                }
                console.log("res", res );
                var data = {
                    type: "addressList",
                    element: ".address",
                    data: res.data
                }
                A.init(data);
            })
        }
        getAddressList();
        $(".btn-confirm").click(function () {
             //声明变量对应需要的判断
             var mobile = $("input[name='mobile']").val(),
                 name = $("input[name='name']").val(),
                 address = $ ("input[name='address']").val(),
                 address_detail = $ ("input[name='address_detail']").val();

            // 姓名不能为空
            if (!name) {
                return $.toast("收件人不能为空", "text");
            }

           // 验证手机号
            var info = InputStrategy.check("isMobile", mobile);

            if (info) {
                return $.toast(info, "text");
            }

            //所在区域不能为空
            if (!address) {
                return $.toast("所在区域不能为空", "text");
            }
            //详细地址不能为空
            if (!address_detail) {
                return $.toast("详细地址不能为空", "text");
            }

            //请求接口，添加地址
            save('/api/user/addAddress', '#info', function (res) {
                console.log("res", res)
                if (res.code == 200) {
                    // 如果添加成功，返回上一页
                    history.go(-1);
                }
            });
        });

        // 根据id拿地址信息
        var id = getRequest().id;

        //修改地址
        function getAddressInfo(id) {
            $.post("/api/user/detailAddress", {id: id}, function (res) {
                console.log("res", JSON.parse(res));
                //根据字段对应修改信息
                res = JSON.parse(res);
                $("input[name='id']").val(id)
                $("input[name='name']").val(res.data.name)
                $("input[name='mobile']").val(res.data.mobile)
                $("input[name='address']").val(res.data.address)
                $("input[name='address_detail']").val(res.data.address_detail)
            });
        }
        getAddressInfo(id);
    </script>

@endsection
